<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 房源信息
 *
 * @icon fa fa-circle-o
 */
class YjyHouse extends Backend
{
    
    /**
     * YjyHouse模型对象
     * @var \app\admin\model\YjyHouse
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\YjyHouse;
        $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("houseStatusList", $this->model->getHouseStatusList());
        //用户
        $userList = db("user")
            ->field("id,mobile")
            ->where('type',2)
            ->select();
        $this->view->assign('userlist',$userList);
        //经纪人
        $agent = db("user")
            ->field("id,mobile,nickname")
            ->where("type",1)
            ->select();
        $this->view->assign("agent",$agent);
        //房东列表
        $landlord = db("yjy_house_landlord")
            ->field("id,name,mobile")
            ->select();
        $this->view->assign("landlord",$landlord);
        //区域
        $area = db("yjy_house_area")
            ->field("id,name")
            ->select();
        $this->view->assign("area",$area);
        //朝向
        $orientations = db("yjy_house_orientations")
            ->field("id,name")
            ->select();
        $this->view->assign("orientations",$orientations);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user','yjyhousetags','yjyhousearea','yjyhouseconfig','yjyhouselandlord'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user','yjyhousetags','yjyhousearea','yjyhouseconfig','yjyhouselandlord'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['Id','title','location_near','image','images','video','price','collect','is_recommend','content','house_area','house_floor','status','createtime','house_status','lon','lat','location_map']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['mobile','nickname']);
				$row->visible(['yjyhousetags']);
				$row->getRelation('yjyhousetags')->visible(['name']);
				$row->visible(['yjyhousearea']);
				$row->getRelation('yjyhousearea')->visible(['name']);
				$row->visible(['yjyhouseconfig']);
				$row->getRelation('yjyhouseconfig')->visible(['name']);
				$row->visible(['yjyhouselandlord']);
				$row->getRelation('yjyhouselandlord')->visible(['name','mobile']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    //预约
    public function order($ids = null){
        if ($this->request->isPost()) {
            $house_id = $this->request->request("house_id");
            $mobile = $this->request->param("mobile_text");
            $ordertime = $this->request->request("ordertime");
            $user_id = db('user')->where("mobile",$mobile)->value("id");
            $agent_id = db('yjy_house')->where('Id',$house_id)->value('user_id');
            if(!$user_id){
                $this->error("用户不存在");
            }
            //添加预约表
            $res = db('yjy_order')->insert([
                'user_id'  => $user_id,
                'house_id' => $house_id,
                'agent_id' => $agent_id,
                'ordertime'=> strtotime($ordertime),
                'createtime'=> time()
            ]);
            if($res){
                $this->success("添加成功");
            }else{
                $this->error('添加失败');
            }
        }
        $agentList = db("user a")
            //->join("user_agent b","a.id=b.User_id","LEFT")
            ->field("id,mobile,nickname")
            ->where("type",1)
            ->select();
        $this->view->assign("agentList",$agentList);
        $this->assign("house_id",$ids);
        return $this->view->fetch();
    }

    //下拉显示
    public function showuser(){
        $this->model = new \app\admin\model\User;
        if ($this->request->request('keyField'))
        {
            return $this->selectpage();
        }
    }



}
